class ClassRoom {
  constructor(address, name, students) {
    this.address = address;
    this.name = name;
    this.students = students;
  }

  entry(newStudent) {
    this.students.push(newStudent);
  }

  [Symbol.iterator]() {
    let index = 0;
    return {
      next: () => {
        if (index < this.students.length) {
          return { done: false, value: this.students[index++] };
        } else {
          return { done: true, value: undefined };
        }
      },
      return: () => {
        console.log("迭代器提前终止了~");
        return { done: true, value: undefined };
      },
    };
  }
}

const classroom = new ClassRoom("8号楼102", "计算机教室", [
  "james",
  "kobe",
  "curry",
  "why",
]);
classroom.entry("frank");

for (const stu of classroom) {
  console.log(stu);
  if (stu == "why") break;
}

const obj = {
  name: "frank",
  age: 18,

  [Symbol.iterator]: function () {
    const keys = Object.keys(this);
    let index = 0;

    return {
      next: () => {
        if (index < keys.length) {
          return { done: false, value: this[keys[index++]] };
        } else {
          return { done: true, value: undefined };
        }
      },
    };
  },
};
for (const item of obj) {
  console.log(item);
}
